/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Controller.AccountManager;
import Controller.LogManager;
import java.io.IOException;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author pauld
 */
@WebServlet(name = "unlockAcct", urlPatterns = {"/unlockAcct"})
public class unlockAcct extends HttpServlet{
    private static String account = null;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //AccountManager.unlockAccount() returns true if the
        //the user account is successfully unlocked
        if( AccountManager.unlockAccount(account) ){
            LogManager.logEvent("UnlockAccount." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.INFO, "Unlock Account Successful: " + account, request.getRemoteAddr(), String.valueOf(request.getSession().getAttribute("name")));
            response.sendRedirect("admin/adminPanel.jsp");
        }
        else{
            LogManager.logEvent("UnlockAccount." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.WARNING, "Unlock Account Unsuccessful", request.getRemoteAddr(), String.valueOf(request.getSession().getAttribute("name")));
            response.sendRedirect("admin/adminPanel.jsp");
        }
    }
    
    //method for validating user inputs
    private static boolean checkInput(HttpServletRequest request){
        account = request.getParameter("acct");
        
        //check validty of user inputs
        //through regex
        String regexAlphaNum = "^[A-Za-z0-9]+$"; //whitelist
        Pattern pAlphaNum = Pattern.compile(regexAlphaNum, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
        Matcher mAcct = pAlphaNum.matcher(account);
        boolean resultAcct = mAcct.find();
        
        if( (account.length() >= 4 && account.length() <= 25) &&
                resultAcct ){
            return true;
        }
        else{
            LogManager.logEvent("UnlockAccount." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.WARNING, "Unlock Account Unsuccessful - Invalid Input", request.getRemoteAddr(), String.valueOf(request.getSession().getAttribute("name")));
            return false;
        }
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //if the user inputs are valid
        //then process the request
        if( checkInput(request) ){
            processRequest(request, response);
        }
        else{
            response.sendRedirect("admin/adminPanel.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //if the user inputs are valid
        //then process the request
        if( checkInput(request) ){
            processRequest(request, response);
        }
        else{
            response.sendRedirect("admin/adminPanel.jsp");
        }
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
